﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
 
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace SinaBlogQiangShafa
{
    public partial class Service1 : ServiceBase
    {
        private static readonly string mobile = System.Configuration.ConfigurationSettings.AppSettings["mobile"];
       // private Timer timer;
        private int rssCount = 0;
        private RunTrigger t = null;
        private string[] rssAddress = null;
       
        private int timespan = 30;// 间隔时间
        public Service1()
        {
            InitializeComponent();
           
            rssAddress = System.Configuration.ConfigurationSettings.AppSettings["rssAddress"].Split('|');
            rssCount = rssAddress.Length;

           
            timespan = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["timespan"]);
            
           
            
        }

        protected override void OnStart( string[] args)
        {
             
           // timer = new System.Threading.Timer(new TimerCallback(timerCall), rssAddress, 0, timespan * 1000);
            
            //ParameterizedThreadStart tsa =null;
           
                for (int i = 0; i < rssAddress.Length; i++)
                {
                    string rss = rssAddress[i].Trim();
                    string title = rss.Substring(rss.LastIndexOf('/') + 1, rss.LastIndexOf('.') - rss.LastIndexOf('/') - 1);
                     
                    Job job = new Job(title);
                    t = new RunTrigger(job, timespan * 1000);
                     t.Run(null, false);

                }
               
            
           
          
        }
        //void timerCall(object obj)
        //{
        //    string[] rssArr = obj as string[];
        //    //ParameterizedThreadStart tsa =null;
        //    for (int i = 0; i < rssArr.Length; i++)
        //    {
        //        string rss = rssArr[i].Trim();
        //        string title = rss.Substring(rss.LastIndexOf('/') + 1, rss.LastIndexOf('.') - rss.LastIndexOf('/') - 1);

        //        Job job = new Job(title);

        //        ThreadPool.QueueUserWorkItem(new WaitCallback(watch), job);



        //        // tsa = new ParameterizedThreadStart(watch);
        //        // thrArr[i] = new Thread(tsa);
        //        //     thrArr[i].IsBackground = true;
        //        // thrArr[i].Start(job);

        //    }

        //}
        void watch(object state)
        {
            
            Job job = state as Job;
           
            job.Run();


        }
        protected override void OnStop()
        {
            //if (timer != null)
            //{
            //    timer.Change(Timeout.Infinite, 0);
            //}
            t.Dispose();
          //  SmsFoFetion.sendMessage("抢新浪博客de沙发服务被退出", mobile);
            
        }
    }
}
